import { getActiveTabStorage, setActiveTabStorage, deleteActiveTabStorage } from "@/utils/storages"

const tabbar = {
  state: {
    tabActived: getActiveTabStorage(), // 当前激活的标签页
    tabPanes: [], // 所有标签页
    tabIncludes: [], // 需要缓存的标签页
    tabExcludes: [] // 需要排除的标签页
  },
  mutations: {
    setActiveTab(state, item) {
      state.tabActived = item
    },
    addTabPanes(state, item) {
      if (item && item.path) {
        let isExist = false
        let index = -1
        for (let i in state.tabPanes) {
          if (state.tabPanes[i].path === item.path) {
            isExist = true
            index = Number(i)
            break
          }
        }
        if (!isExist) {
          state.tabPanes.push(item)
        } else {
          state.tabPanes[index].query = item.query || {}
        }
      } else {
        console.log("数据错误!")
      }
    },
    deleteTabPanes(state, path) {
      for (let i in state.tabPanes) {
        //删除当前，并设置前一个对象为当前的
        if (state.tabPanes[i].path === path) {
          if (state.tabActived && state.tabActived.path === path) {
            if (i > 0) {
              state.tabActived = state.tabPanes[Number(i) - 1]
            } else {
              state.tabActived = state.tabPanes[Number(i) + 1]
            }
            if (state.tabActived) {
              setActiveTabStorage(state.tabActived)
            } else {
              deleteActiveTabStorage()
            }
          }
          state.tabPanes.splice(i, 1)
        }
      }
    },
    deleteTabPanesArray(state, arr) {
      state.tabPanes.splice(...arr)
    },
    addTagIncludes(state, item) {
      if (item && item.name) {
        let isExist = false
        for (let i in state.tabIncludes) {
          if (state.tabIncludes[i] === item.name) {
            isExist = true
            break
          }
        }
        if (!isExist) {
          state.tabIncludes.push(item.name)
        }
      } else {
        console.log("数据错误!")
      }
    },
    deleteTagIncludes(state, names) {
      //删除指定的的tags
      for (let i in names) {
        let name = names[i]
        let index = state.tabIncludes.indexOf(name)
        if (index !== -1) {
          state.tabIncludes.splice(index, 1)
        }
      }
    },
    addTagExcludes(state, item) {
      if (item && item.name) {
        let isExist = false
        for (let i in state.tabExcludes) {
          if (state.tabExcludes[i] === item.name) {
            isExist = true
            break
          }
        }
        if (!isExist) {
          state.tabExcludes.push(item.name)
        } 
      } else {
        console.log("数据错误!")
      }
    },
    deleteTagExcludes(state, name) {
      for (let i in state.tabExcludes) {
        //删除当前的tag
        if (state.tabExcludes[i] === name) {
          state.tabExcludes.splice(i, 1)
        }
      }
    }
  },
  actions: {
    setActiveTab({ commit }, item) {
      return new Promise(resolve => {
        commit("setActiveTab", item)
        setActiveTabStorage(item)
        resolve()
      })
    },
    addTabPanes({ commit }, item) {
      return new Promise(resolve => {
        commit("addTabPanes", item)
        resolve()
      })
    },
    deleteTabPanes({ commit }, path) {
      return new Promise(resolve => {
        commit("deleteTabPanes", path)
        resolve()
      })
    },
    deleteTabPanesArray({ commit }, arr) {
      return new Promise(resolve => {
        commit("deleteTabPanesArray", arr)
        resolve()
      })
    },
    addTagIncludes({ commit }, item) {
      return new Promise(resolve => {
        commit("addTagIncludes", item)
        resolve()
      })
    },
    deleteTagIncludes({ commit }, name) {
      return new Promise(resolve => {
        commit("deleteTagIncludes", name)
        resolve()
      })
    },
    addTagExcludes({ commit }, item) {
      return new Promise(resolve => {
        commit("addTagExcludes", item)
        resolve()
      })
    },
    deleteTagExcludes({ commit }, name) {
      return new Promise(resolve => {
        commit("deleteTagExcludes", name)
        resolve()
      })
    }
  }
}
export default tabbar


